package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.R;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.BindView;
import butterknife.OnClick;

public class BreLanjianPasteBarCodeOutPutActivity  extends BaseActivity {

    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.xinghao)
    FreeUI_ClearEditText xinghao;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.fengzuang)
    TextView fengzuang;
    @BindView(R.id.custcode)
    TextView custcode;
    @BindView(R.id.shuliang)
    TextView shuliang;
    @BindView(R.id.etWoId)
    EditText etWoId;
    @BindView(R.id.base_reel_num)
    TextView base_reel_num;
    @BindView(R.id.bar_code_num)
    TextView bar_code_num;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;

    // 日期，操作工，账号，封装，客户代码，型号，工单，批号，数量，盘基数，批次二维码数
    private String SEND_TIME;
//    private String WO_ID;
    private String LOT_ID;
    private int MAIN_QTY=0;
    private int BASE_REEL_NUM=0;
    private int LOT_BAR_NUM=0;
    private String CUSTOMER_CODE;
    private String MAIN_MAT_TYPE;
    private String PRODUCT_MODEL;
    private String USER_NAME;  // 略
    private String NAME;  // 略

    private LotAndPackagingfileEntity entity;
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private LotInfoEntity lotInfoEntity;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bre_lanjian_paste_bar_code_out_put;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);

        initEdit(etLotID, sa -> {
            //获取lot信息
//            LotUtils.searchLotInfo(getApplicationContext(), sa, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
//                @Override
//                public void onSuccess(LotInfoEntity body) {
//                    lotInfoEntity = body;
//                    if(body.getLOT() != null || !body.getLOT().equals("null") ){
//                        fengzuang.setText(body.getLOT().getMAINMATTYPE());
//                        shuliang.setText(body.getLOT().getMAINQTY());
//                        if(body.getLOT().getPROCEDURENAME() != null){
//                            xinghao.setText(body.getLOT().getUDF().getProductModel());
//                        }else{
//                            Toast.makeText(getApplicationContext(), "该批次型号为空", Toast.LENGTH_SHORT).show();
//                        }
//                        etWoId.setText(body.getLOT().getWOID());
//                        custcode.setText(body.getLOT().getCUSTOMERCODE());
//                        MAIN_QTY=Integer.parseInt(body.getLOT().getMAINQTY());
//                        CUSTOMER_CODE=body.getLOT().getCUSTOMERCODE();
//                    }
//                }
//                @Override
//                public void onFail(Throwable t) {
//
//                }
//            });
            search();
        });

    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                submit();
                break;
        }
    }
    private void btnClean1(){
        etLotID.setText(null);
        xinghao.setText(null);
        fengzuang.setText(null);
        shuliang.setText(null);
        base_reel_num.setText(null);
        bar_code_num.setText(null);
        custcode.setText(null);
        etWoId.setText(null);
    }

    private void submit(){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SEND_TIME = format.format(new Date());
        USER_NAME= Constant.CURRENT_USER;
        NAME=Constant.CURRENT_DESC;
        if(getText(etLotID).equals("")){
            showToast("请先扫描批次信息", false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("成功", true);
                    btnClean1();
                    etLotID.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PDA_QUERY_LOT_PACKS");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("WO_ID", getText(etWoId));
        map.addProperty("LOT_ID", getText(etLotID));
        map.addProperty("MAIN_QTY", MAIN_QTY);
        map.addProperty("BASE_REEL_NUM", BASE_REEL_NUM);
        map.addProperty("LOT_BAR_NUM", LOT_BAR_NUM);
        map.addProperty("CUSTOMER_CODE", custcode.getText().toString().trim());
        map.addProperty("MAIN_MAT_TYPE", fengzuang.getText().toString().trim());
        map.addProperty("PRODUCT_MODEL", xinghao.getText().toString().trim());
        map.addProperty("USER_NAME", Constant.CURRENT_USER);
        map.addProperty("NAME", Constant.CURRENT_DESC);
        map.addProperty("SEND_TIME", SEND_TIME);
        map.addProperty("ACTION_TYPE", "SAVE");
        NetUtils.requestPostNet("IF.PDA_QUERY_LOT_PACKS", "Common", BreLanjianPasteBarCodeOutPutActivity.this, map, callBack);
    }

    private void search(){
        FreeUI_EntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>>
                (new TypeToken<BaseResponseBean<LotAndPackagingfileEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    entity = response.body().getResponse().getBody();
                    inputvalue(entity);
                    showToast("成功", true);
                    etLotID.requestFocus();
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "失败");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("LOT_ID", getText(etLotID));
        map.addProperty("ACTION_TYPE", "QUERY");
        NetUtils.requestPostNet("IF.PDA_QUERY_LOT_PACKS", "Common", BreLanjianPasteBarCodeOutPutActivity.this, map, callBack);
    }

    private void  inputvalue(LotAndPackagingfileEntity entity){
        SEND_TIME = format.format(new Date());
//        WO_ID = entity.getLOT().getWOID();
//        LOT_ID = entity.getLOT().getLOTID();
        MAIN_QTY = Integer.parseInt(entity.getLOT().getMAINQTY());
        BASE_REEL_NUM = Integer.parseInt(entity.getPACKS().getReelNum());
        LOT_BAR_NUM = (int) Math.ceil(MAIN_QTY * 1.0 / BASE_REEL_NUM);
        CUSTOMER_CODE = entity.getLOT().getCUSTOMERCODE();
        MAIN_MAT_TYPE = entity.getLOT().getMAINMATTYPE();
        PRODUCT_MODEL = entity.getLOT().getUDF().getProductModel();
//        etLotID.setText(LOT_ID);
        etWoId.setText(entity.getLOT().getWOID());
        xinghao.setText(PRODUCT_MODEL);
        fengzuang.setText(MAIN_MAT_TYPE);
        custcode.setText(CUSTOMER_CODE);
        shuliang.setText(String.valueOf(MAIN_QTY));
        base_reel_num.setText(String.valueOf(BASE_REEL_NUM));
        bar_code_num.setText(" ≈ "+LOT_BAR_NUM);
    }

}